<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('sales.salesorders.orderno')" type="number" prop="orderno" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.orderno" :placeholder="t('Please input field', { field: t('sales.salesorders.orderno') })" />
                    <FormItem :label="t('sales.salesorders.debtorno')" type="remoteSelect" v-model="baTable.form.items!.debtorno" prop="debtorno" :input-attr="{ pk: 'cncerp_sales_debtorsmaster.debtorno', field: 'name', 'remote-url': '/admin/sales.Debtorsmaster/index' }" :placeholder="t('Please select field', { field: t('sales.salesorders.debtorno') })" />
                    <FormItem :label="t('sales.salesorders.branchcode')" type="remoteSelect" v-model="baTable.form.items!.branchcode" prop="branchcode" :input-attr="{ pk: 'cncerp_sales_custbranch.branchcode', field: 'brname', 'remote-url': '/admin/sales.Custbranch/index' }" :placeholder="t('Please select field', { field: t('sales.salesorders.branchcode') })" />
                    <FormItem :label="t('sales.salesorders.customerref')" type="string" v-model="baTable.form.items!.customerref" prop="customerref" :placeholder="t('Please input field', { field: t('sales.salesorders.customerref') })" />
                    <FormItem :label="t('sales.salesorders.buyername')" type="string" v-model="baTable.form.items!.buyername" prop="buyername" :placeholder="t('Please input field', { field: t('sales.salesorders.buyername') })" />
                    <FormItem :label="t('sales.salesorders.comments')" type="string" v-model="baTable.form.items!.comments" prop="comments" :placeholder="t('Please input field', { field: t('sales.salesorders.comments') })" />
                    <FormItem :label="t('sales.salesorders.orddate')" type="datetime" v-model="baTable.form.items!.orddate" prop="orddate" :placeholder="t('Please select field', { field: t('sales.salesorders.orddate') })" />
                    <FormItem :label="t('sales.salesorders.ordertype')" type="string" v-model="baTable.form.items!.ordertype" prop="ordertype" :placeholder="t('Please input field', { field: t('sales.salesorders.ordertype') })" />
                    <FormItem :label="t('sales.salesorders.shipvia')" type="remoteSelect" v-model="baTable.form.items!.shipvia" prop="shipvia" :input-attr="{ pk: 'cncerp_sales_shippers.shipper_id', field: 'shippername', 'remote-url': '/admin/sales.Shippers/index' }" :placeholder="t('Please select field', { field: t('sales.salesorders.shipvia') })" />
                    <FormItem :label="t('sales.salesorders.deladd1')" type="string" v-model="baTable.form.items!.deladd1" prop="deladd1" :placeholder="t('Please input field', { field: t('sales.salesorders.deladd1') })" />
                    <FormItem :label="t('sales.salesorders.deladd2')" type="string" v-model="baTable.form.items!.deladd2" prop="deladd2" :placeholder="t('Please input field', { field: t('sales.salesorders.deladd2') })" />
                    <FormItem :label="t('sales.salesorders.deladd3')" type="string" v-model="baTable.form.items!.deladd3" prop="deladd3" :placeholder="t('Please input field', { field: t('sales.salesorders.deladd3') })" />
                    <FormItem :label="t('sales.salesorders.deladd4')" type="string" v-model="baTable.form.items!.deladd4" prop="deladd4" :placeholder="t('Please input field', { field: t('sales.salesorders.deladd4') })" />
                    <FormItem :label="t('sales.salesorders.deladd5')" type="string" v-model="baTable.form.items!.deladd5" prop="deladd5" :placeholder="t('Please input field', { field: t('sales.salesorders.deladd5') })" />
                    <FormItem :label="t('sales.salesorders.deladd6')" type="string" v-model="baTable.form.items!.deladd6" prop="deladd6" :placeholder="t('Please input field', { field: t('sales.salesorders.deladd6') })" />
                    <FormItem :label="t('sales.salesorders.contactphone')" type="string" v-model="baTable.form.items!.contactphone" prop="contactphone" :placeholder="t('Please input field', { field: t('sales.salesorders.contactphone') })" />
                    <FormItem :label="t('sales.salesorders.contactemail')" type="string" v-model="baTable.form.items!.contactemail" prop="contactemail" :placeholder="t('Please input field', { field: t('sales.salesorders.contactemail') })" />
                    <FormItem :label="t('sales.salesorders.deliverto')" type="string" v-model="baTable.form.items!.deliverto" prop="deliverto" :placeholder="t('Please input field', { field: t('sales.salesorders.deliverto') })" />
                    <FormItem :label="t('sales.salesorders.deliverblind')" type="number" prop="deliverblind" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.deliverblind" :placeholder="t('Please input field', { field: t('sales.salesorders.deliverblind') })" />
                    <FormItem :label="t('sales.salesorders.freightcost')" type="number" prop="freightcost" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.freightcost" :placeholder="t('Please input field', { field: t('sales.salesorders.freightcost') })" />
                    <FormItem :label="t('sales.salesorders.fromstkloc')" type="remoteSelect" v-model="baTable.form.items!.fromstkloc" prop="fromstkloc" :input-attr="{ pk: 'cncerp_stock_locations.loccode', field: 'locationname', 'remote-url': '/admin/stock.Locations/index' }" :placeholder="t('Please select field', { field: t('sales.salesorders.fromstkloc') })" />
                    <FormItem :label="t('sales.salesorders.deliverydate')" type="datetime" v-model="baTable.form.items!.deliverydate" prop="deliverydate" :placeholder="t('Please select field', { field: t('sales.salesorders.deliverydate') })" />
                    <FormItem :label="t('sales.salesorders.confirmeddate')" type="datetime" v-model="baTable.form.items!.confirmeddate" prop="confirmeddate" :placeholder="t('Please select field', { field: t('sales.salesorders.confirmeddate') })" />
                    <FormItem :label="t('sales.salesorders.printedpackingslip')" type="number" prop="printedpackingslip" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.printedpackingslip" :placeholder="t('Please input field', { field: t('sales.salesorders.printedpackingslip') })" />
                    <FormItem :label="t('sales.salesorders.datepackingslipprinted')" type="datetime" v-model="baTable.form.items!.datepackingslipprinted" prop="datepackingslipprinted" :placeholder="t('Please select field', { field: t('sales.salesorders.datepackingslipprinted') })" />
                    <FormItem :label="t('sales.salesorders.quotation')" type="number" prop="quotation" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.quotation" :placeholder="t('Please input field', { field: t('sales.salesorders.quotation') })" />
                    <FormItem :label="t('sales.salesorders.quotedate')" type="datetime" v-model="baTable.form.items!.quotedate" prop="quotedate" :placeholder="t('Please select field', { field: t('sales.salesorders.quotedate') })" />
                    <FormItem :label="t('sales.salesorders.poplaced')" type="number" prop="poplaced" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.poplaced" :placeholder="t('Please input field', { field: t('sales.salesorders.poplaced') })" />
                    <FormItem :label="t('sales.salesorders.salesperson')" type="remoteSelect" v-model="baTable.form.items!.salesperson" prop="salesperson" :input-attr="{ pk: 'cncerp_sales_salesman.salesmanname', field: 'salesmanname', 'remote-url': '/admin/sales.Salesman/index' }" :placeholder="t('Please select field', { field: t('sales.salesorders.salesperson') })" />
                    <FormItem :label="t('sales.salesorders.internalcomment')" type="string" v-model="baTable.form.items!.internalcomment" prop="internalcomment" :placeholder="t('Please input field', { field: t('sales.salesorders.internalcomment') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    orderno: [buildValidatorData({ name: 'number', title: t('sales.salesorders.orderno') }), buildValidatorData({ name: 'required', title: t('sales.salesorders.orderno') })],
    debtorno: [buildValidatorData({ name: 'required', title: t('sales.salesorders.debtorno') })],
    branchcode: [buildValidatorData({ name: 'required', title: t('sales.salesorders.branchcode') })],
    orddate: [buildValidatorData({ name: 'date', title: t('sales.salesorders.orddate') })],
    shipvia: [buildValidatorData({ name: 'required', title: t('sales.salesorders.shipvia') })],
    contactemail: [buildValidatorData({ name: 'email', title: t('sales.salesorders.contactemail') })],
    deliverblind: [buildValidatorData({ name: 'number', title: t('sales.salesorders.deliverblind') })],
    freightcost: [buildValidatorData({ name: 'number', title: t('sales.salesorders.freightcost') })],
    deliverydate: [buildValidatorData({ name: 'date', title: t('sales.salesorders.deliverydate') })],
    confirmeddate: [buildValidatorData({ name: 'date', title: t('sales.salesorders.confirmeddate') })],
    printedpackingslip: [buildValidatorData({ name: 'number', title: t('sales.salesorders.printedpackingslip') })],
    datepackingslipprinted: [buildValidatorData({ name: 'date', title: t('sales.salesorders.datepackingslipprinted') })],
    quotation: [buildValidatorData({ name: 'number', title: t('sales.salesorders.quotation') })],
    quotedate: [buildValidatorData({ name: 'date', title: t('sales.salesorders.quotedate') })],
    poplaced: [buildValidatorData({ name: 'number', title: t('sales.salesorders.poplaced') })],
})
</script>

<style scoped lang="scss"></style>
